package com.xbs.controller.user;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.UserEntity;
import com.xbs.service.user.RegService;
@WebServlet("/reg")
public class RegServlet extends BaseServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//生成随机四位验证码
		int num = (int) Math.floor(Math.random()*9000+1000);
		//清除当前会话
		req.getSession().invalidate();
		//生成验证码
		req.setAttribute("yzm", num);
		//转发到jsp页面
		forward("userview/reg");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		RegService us = new RegService();
		
		//获取注册的用户对象
		UserEntity user = new UserEntity();
		String uname = req.getParameter("xname");
		String ucard = req.getParameter("card");
		String uphone = req.getParameter("telnum");
		String upwd = req.getParameter("pwd");
		String upay = req.getParameter("paypwd");
		
		user.setUser_name(uname);
		user.setUser_idcard(ucard);
		user.setUser_telphone(uphone);
		user.setUser_password(upwd);
		user.setUser_paypassword(upay);
		//将用户对象插入数据库
		us.insertuser(user);
		HashMap<String, Object> map = new HashMap<>();
		map.put("uname", uname);
		map.put("ucard", ucard);
		map.put("uphone",uphone);
		map.put("upwd", upwd);
		map.put("upay", upay);
		req.getSession().setAttribute("statu", 1);
		//注册成功之后跳转到首页
		redirect("/login",map);
	}
}
